package com.hangsu.train.components.web.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hangsu.train.core.result.Resp;
import com.hangsu.train.netty.ClientHolder;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class SentController {

    @Autowired
    private ObjectMapper objectMapper;

    @RequestMapping("/send")
    public Resp<?> send() throws InterruptedException, JsonProcessingException {
        Map<String,String> map=new HashMap<>();
        map.put("tag","hello");
        ClientHolder.getSocketClient().writeAndFlush(new TextWebSocketFrame(objectMapper.writeValueAsString(map))).sync();
        return Resp.ok();
    }
}
